﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Worktool.CODE_Chris.FileClasses;

namespace Worktool.CODE_Chris.Forms.Update
{
    public partial class SearchForUpdates : Form
    {
        public string returnValue { get; set; }
        public static List<string> info = new List<string>();
        private BackgroundWorker bw;
        private int updateAvailable = 0;

        //Updateinformationen
        public static String downloadUrl = "ftp://Amptown_Worktool:Asc123!654@78.46.232.75/";
        public static String versionFileName = "UM.txt";
        public static String versionNr = "";
        public static String updaterPrefix = "MV000_";

        public SearchForUpdates()
        {
            this.ShowInTaskbar = false;
            InitializeComponent();
            init();
        }

        private void init()
        {
            versionNr = getVersion();

            //Ini-Datei auslesen und ConnectionString setzen
            new GetXmlFile();

            bw = new BackgroundWorker();

            bw.DoWork -= new DoWorkEventHandler(bw_download_DoWork);
            bw.DoWork += new DoWorkEventHandler(bw_download_DoWork);

            bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

            bw.WorkerReportsProgress = true;
            bw.ProgressChanged += bw_download_reportProgress;

            bw.WorkerSupportsCancellation = true;
            bw.RunWorkerAsync();
        }

        private void bw_download_DoWork(object sender, DoWorkEventArgs e)
        {
            checkForUpdate();
        }

        private void bw_download_reportProgress(object sender, ProgressChangedEventArgs e)
        {

        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (updateAvailable == -1)
            {
                //Verbindungsfehler
                this.Visible = false;
                MessageBox.Show("Es konnte keine Verbindung zum Update-Server hergestellt werden. Bitte prüfen Sie Ihre Internetverbindung.", "Achtung");
                this.Close();
            }
            else if (updateAvailable == 1)
            {
                updateProgram();
            }
            else
            {
                this.Close();
            }
        }

        private void checkForUpdate()
        {
            //Zuvor checken, ob die Update.exe aktualisiert werden muss
            updateUpdateExe();

            //Infos zur aktuellen Version vom Server ziehen
            info = UpdateProgram.getUpdateInfo(downloadUrl, versionFileName, Login.dictSettings["apppath"] + "Updates\\", 1);

            if (info == null)
            {
                //Verbindungsfehler
                updateAvailable = -1;
            }
            else
            {
                long newVersionNr = Convert.ToInt64(info[1].Replace(".", ""));
                long oldVersionNr = Convert.ToInt64(versionNr.Replace(".", ""));

                if (newVersionNr > oldVersionNr)
                {
                    //Neue Version verfügbar
                    updateAvailable = 1;
                }
                else
                {
                    //Keine neue Version verfügbar
                    updateAvailable = 0;
                }
            }
        }

        private void updateUpdateExe()
        {
            UpdateProgram.updateMe(updaterPrefix, Login.dictSettings["apppath"]);
            
        }

        private void updateProgram()
        {
            this.Visible = false;

            UpdateProgram.installUpdateRestart(info[3], info[4], "\"" + Login.dictSettings["apppath"] + "Updates\\", "Worktool", Login.dictSettings["apppath"] + "Worktool.exe", "updated", Login.dictSettings["apppath"] + "\\Update.exe");
            System.Diagnostics.Process.GetCurrentProcess().Kill();
            Application.Exit();

        }

        private String getVersion()
        {
            String versionTemp = "";
            versionTemp += System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + ".";
            versionTemp += System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString() + ".";
            versionTemp += System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build.ToString() + ".";
            versionTemp += System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision.ToString();

            return versionTemp;
        }
    }
}
